﻿using System;
using System.Linq;

namespace Kent.Boogaart.Truss.Specifications.Support.Converters
{
	public sealed class NameAndAddressMultiValueConverter : IMultiValueConverter
	{
		public object ConvertSourcesToTarget(object[] values, Type type, object parameter)
		{
			if (values.All(x => x == null))
			{
				return null;
			}

			return string.Join("~", values.Select(x => x == null ? "" : x.ToString()).ToArray());
		}

		public object[] ConvertTargetToSources(object value, Type[] types, object parameter)
		{
			var valueStr = value as string;

			if (valueStr == null)
			{
				return new object[types.Length];
			}

			return (value as string).Split('~');
		}
	}
}